package com.example;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Renderer;
import android.os.Bundle;

import com.step.Step;
import com.step.Step.Dimension;
import com.step.mesh.Mesh;
import com.step.sprite.Sprite.RenderType;

public class MeshSandbox extends Activity implements Renderer {
	GLSurfaceView glSurfaceView;
	Mesh mesh;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		glSurfaceView = new GLSurfaceView(this);
		glSurfaceView.setRenderer(this);
		setContentView(glSurfaceView);
		
		mesh = new Mesh(Dimension.d3d, 24, 12, true, false, false);
		// 前面
		mesh.addVertex(-0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 0, 1);
		mesh.addVertex(0.5f, -0.5f, 0.5f, 255, 0, 0, 255, 1, 1);
		mesh.addVertex(0.5f, 0.5f, 0.5f, 255, 0, 0, 255, 1, 0);
		mesh.addVertex(-0.5f, 0.5f, 0.5f, 255, 0, 0, 255, 0, 0);

		// 右面
		mesh.addVertex(0.5f, -0.5f, 0.5f, 0, 255, 0, 255, 0, 1);
		mesh.addVertex(0.5f, -0.5f, -0.5f, 0, 255, 0, 255, 1, 1);
		mesh.addVertex(0.5f, 0.5f, -0.5f, 0, 255, 0, 255, 1, 0);
		mesh.addVertex(0.5f, 0.5f, 0.5f, 0, 255, 0, 255, 0, 0);

		// 后面
		mesh.addVertex(0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f, 0.5f, -0.5f, 0, 0, 255, 255, 1, 0);
		mesh.addVertex(0.5f, 0.5f, -0.5f, 0, 0, 255, 255, 0, 0);

		// 左面
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 255, 0, 255, 255, 0, 1);
		mesh.addVertex(-0.5f, -0.5f, 0.5f, 255, 0, 255, 255, 1, 1);
		mesh.addVertex(-0.5f, 0.5f, 0.5f, 255, 0, 255, 255, 1, 0);
		mesh.addVertex(-0.5f, 0.5f, -0.5f, 255, 0, 255, 255, 0, 0);

		// 上面
		mesh.addVertex(-0.5f, 0.5f, 0.5f, 255, 255, 0, 255, 0, 1);
		mesh.addVertex(0.5f, 0.5f, 0.5f, 255, 255, 0, 255, 1, 1);
		mesh.addVertex(0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 1, 0);
		mesh.addVertex(-0.5f, 0.5f, -0.5f, 255, 255, 0, 255, 0, 0);

		// 下面
		mesh.addVertex(-0.5f, -0.5f, 0.5f, 0, 255, 255, 255, 0, 1);
		mesh.addVertex(0.5f, -0.5f, 0.5f, 0, 255, 255, 255, 1, 1);
		mesh.addVertex(0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 1, 0);
		mesh.addVertex(-0.5f, -0.5f, -0.5f, 0, 255, 255, 255, 0, 0);

		// 索引
		mesh.addFace(0, 1, 3);
		mesh.addFace(1, 2, 3);
		mesh.addFace(4, 5, 7);
		mesh.addFace(5, 6, 7);
		mesh.addFace(8, 9, 11);
		mesh.addFace(9, 10, 11);
		mesh.addFace(12, 13, 15);
		mesh.addFace(13, 14, 15);
		mesh.addFace(16, 17, 19);
		mesh.addFace(17, 18, 19);
		mesh.addFace(20, 21, 23);
		mesh.addFace(21, 22, 23);
		
		Step.dimension = Dimension.d2d;
	}

	float rotation = 1;

	@Override
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(1, 1, 1, 1);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glTranslatef(0, 0, -0.5f);
		rotation += 0.5f;
		gl.glRotatef(rotation, 1, 0, 0);
		gl.glRotatef(rotation, 0, 1, 0);
		gl.glRotatef(rotation, 0, 0, 1);
		gl.glScalef(1.5f, 1.5f, 1.5f);
		mesh.render(gl, RenderType.TRIANGLES);
		gl.glPopMatrix();
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrthof(-width / 2, width / 2, -height / 2, height / 2, 1, -1);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// 启用阴影平滑
		gl.glShadeModel(GL10.GL_SMOOTH);

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearDepthf(1.0f);
		gl.glDepthFunc(GL10.GL_LESS);
		gl.glDepthRangef(0, 1f);
		gl.glDepthMask(true);

		// 告诉系统对透视进行修正
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
	}
}
